package com.chif.goingplus.service;

import com.chif.goingplus.dao.*;
import com.chif.goingplus.pojo.*;
import com.chif.goingplus.utils.DateUtils;
import com.chif.goingplus.utils.JsonUtils;
import com.chif.goingplus.utils.PercentUtil;
import com.chif.goingplus.websocket.WebSocket;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ClientMapper clientMapper;

    @Autowired
    private BusMapper busMapper;

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private OriginMapper originMapper;

    @Autowired
    private DestinationMapper destinationMapper;

    @Autowired
    WebSocket webSocket;

    /**
     * 1.增加新的订单
     * 2.将订单信息通过ws异步返回
     * @param order
     * @return
     */
    @Override
    @Async
    public int addOrder(Order order) {
        orderMapper.addOrder(order);
        Message message = new Message("fullScreen","newOrder","null",JsonUtils.getJson(order));
        webSocket.sendAllMessage(JsonUtils.getJson(message));
        return order.getOrid();
    }

    @Override
    public int deleteOrderById(int id) {
        return orderMapper.deleteOrderById(id);
    }

    @Override
    public int updateOrder(Order order) {
        return orderMapper.updateOrder(order);
    }

    @Override
    public Order queryOrderById(int id) {
        Order order = orderMapper.queryOrderById(id);
        order.setClient(clientMapper.queryClientById(order.getClientid()));
        order.setBus(busMapper.queryBusById(order.getBusid()));
        order.setSeller(empMapper.queryEmpById(order.getSellerid()));
        return order;
    }

    @Override
    public List<Order> queryAllOrder() {
        List<Order> orders = orderMapper.queryAllOrder();
        for (Order order : orders) {
            order.setClient(clientMapper.queryClientById(order.getClientid()));
            order.setBus(busMapper.queryBusById(order.getBusid()));
            order.setSeller(empMapper.queryEmpById(order.getSellerid()));
        }
        return orders;
    }

    @Override
    public PageInfo<Order> pageHelperOrder(int currentPage,int pageSize) {

        //System.out.println(currentPage+"-------"+pageSize);  //第几页 --- 总页数

        // 分页查询（紧跟在其后的第一条查询sql将会被分页）
        PageHelper.startPage(currentPage,pageSize);
        List<Order> orders = orderMapper.queryAllOrderDESC();
        for (Order order : orders) {
            order.setClient(clientMapper.queryClientById(order.getClientid()));
            order.setBus(busMapper.queryBusById(order.getBusid()));
            order.setSeller(empMapper.queryEmpById(order.getSellerid()));
            order.setOrigin(originMapper.queryOriginById(Integer.parseInt(order.getOrigin())).getOriname());
            order.setDestination(destinationMapper.queryDestinationById(Integer.parseInt(order.getDestination())).getDename());
        }
        PageInfo<Order> pageInfo = new PageInfo<Order>(orders);
        return pageInfo;
    }

    @Override
    public PageInfo<Order> searchOrder(Search search) {
        PageHelper.startPage(search.getCurrentPage(),search.getPageSize());
        String name=search.getName();
        if (name!=null && name!=""){
            Client client = clientMapper.queryClientByName(name);
            if(client==null){//查询名字没有结果
                return new PageInfo<Order>(new ArrayList<Order>());
            }else{//查询名字有结果
                search.setClid(client.getClid());
            }
        }
        List<Order> orders = orderMapper.searchOrder(search);
        for (Order order : orders) {
            order.setClient(clientMapper.queryClientById(order.getClientid()));
            order.setBus(busMapper.queryBusById(order.getBusid()));
            order.setSeller(empMapper.queryEmpById(order.getSellerid()));
            order.setOrigin(originMapper.queryOriginById(Integer.parseInt(order.getOrigin())).getOriname());
            order.setDestination(destinationMapper.queryDestinationById(Integer.parseInt(order.getDestination())).getDename());
        }
        PageInfo<Order> pageInfo = new PageInfo<Order>(orders);
        return pageInfo;
    }

    @Override
    public List<SellerOrder> querySellerOrder() {
        return orderMapper.querySellerOrder();
    }

    @Override
    public Trend<PosMonth> getOrgMonthTrend() {
        Trend<PosMonth> posMonthTrend = new Trend<>();
        posMonthTrend.setTitle("客源分析趋势");
        posMonthTrend.setBase(300);
        posMonthTrend.setUnit("万");
        List<PosMonth> posMonths = new ArrayList<PosMonth>();
        //封装每个出发地点每个月的数据
        PosMonth zzgs = new PosMonth("郑州工商学院",orderMapper.queryOriginMonthCount("3"));//出发地为郑州工商学院每个月的数据
        PosMonth zzjt = new PosMonth("河南交通学院",orderMapper.queryOriginMonthCount("2"));//出发地为河南交通学院每个月的数据
        PosMonth zkzx = new PosMonth("周口中心站",orderMapper.queryOriginMonthCount("4"));//出发地为周口中心站每个月的数据
        PosMonth zzly = new PosMonth("郑州旅游学院",orderMapper.queryOriginMonthCount("1"));//出发地为郑州旅游学院每个月的数据
        //数据封装进趋势图

        System.out.println(orderMapper.queryOriginMonthCount("1"));
        posMonths.add(zzgs);
        posMonths.add(zzjt);
        posMonths.add(zkzx);
        posMonths.add(zzly);
        posMonthTrend.setData(posMonths);
        return posMonthTrend;
    }

    @Override
    public Trend<PosMonth> getDisMonthTrend() {
        Trend<PosMonth> posMonthTrend = new Trend<>();
        posMonthTrend.setTitle("去向分析趋势");
        posMonthTrend.setBase(300);
        posMonthTrend.setUnit("万");
        List<PosMonth> posMonths = new ArrayList();
        //封装每个去向地点每个月的数据
        PosMonth zzgs = new PosMonth("周口火车站",orderMapper.queryDestinationMonthCount("周口火车站"));//目的地为周口火车站每个月的数据
        PosMonth zzjt = new PosMonth("周口市中心站",orderMapper.queryDestinationMonthCount("周口市中心站"));//目的地为周口市中心站每个月的数据
        PosMonth zkzx = new PosMonth("周口市淮阳",orderMapper.queryDestinationMonthCount("周口市淮阳"));//目的地为周口市淮阳每个月的数据
        PosMonth zzly = new PosMonth("周口市项城",orderMapper.queryDestinationMonthCount("周口市项城"));//目的地为周口市项城每个月的数据
        //数据封装进趋势图
        posMonths.add(zzgs);
        posMonths.add(zzjt);
        posMonths.add(zkzx);
        posMonths.add(zzly);
        posMonthTrend.setData(posMonths);
        return posMonthTrend;
    }

    @Override
    public List<RegionOrder> getRegionOrder() {
        return orderMapper.getRegionOrder();
    }

    @Override
    public List<Hots> getHotList() {
        List<Hots> hots = new ArrayList<>();

        //封装地区销量占比图数据
        List<RegionOrder> regionOrder = orderMapper.getRegionOrder();
        hots.add(new Hots("地区销量",regionOrder));

        //封装出发时间占比图数据
        List<Hot> beginTimeOrder = orderMapper.getBeginTimeOrder();
        hots.add(new Hots("出发时间",beginTimeOrder));


        //封装售票员
        List<SellerOrder> sellerOrders = orderMapper.querySellerOrder();
        hots.add(new Hots("售票员业绩",sellerOrders));

        //封装客源分析
        List<OriginOrder> originOrder = orderMapper.getOriginOrder();
        hots.add(new Hots("客源分析",originOrder));

        return hots;
    }

    @Override
    public int getAllOrderCount() {
        return orderMapper.getAllOrderCount();
    }

    @Override
    public List getOrderAndIncomeCount() {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(orderMapper.getAllOrderCount());
        list.add(orderMapper.getCostCount());
        return list;
    }

    @Override
    public List getSaleCount() {
        ArrayList<Object> list = new ArrayList<>();
        list.add(orderMapper.getCostCount());//总销售额

        int toDayCostCount = orderMapper.getToDayCostCount();
        list.add(toDayCostCount);//日销售额

        //日同比
        int yesDayCostCount = orderMapper.getYesDayCostCount();
        if (yesDayCostCount!=0){
            String day = PercentUtil.myPercent(toDayCostCount-yesDayCostCount, yesDayCostCount);
            list.add(day);
        }else{
            list.add("0%");
        }

        //周同比
        int toWeekCostCount = orderMapper.getToWeekCostCount();
        int yesWeekCostCount = orderMapper.getYesWeekCostCount();
        if (yesWeekCostCount!=0){
            String week = PercentUtil.myPercent(toWeekCostCount-yesWeekCostCount, yesWeekCostCount);
            list.add(week);
        }else{
            list.add("0%");
        }

        return list;
    }

    @Override
    public List getPayCount() {
        ArrayList<Object> list = new ArrayList<>();
        list.add(orderMapper.getPayCount());//总销售额

        int toDayPayCount = orderMapper.getToDayPayCount();
        list.add(toDayPayCount);//日销售额

        //日同比
        int yesDayPayCount = orderMapper.getYesDayPayCount();
        if (yesDayPayCount!=0){
            String day = PercentUtil.myPercent(toDayPayCount-yesDayPayCount, yesDayPayCount);
            list.add(day);
        }else{
            list.add("0%");
        }

        //周同比
        int toWeekPayCount = orderMapper.getToWeekPayCount();
        int yesWeekPayCount = orderMapper.getYesWeekPayCount();
        if (yesWeekPayCount!=0){
            String week = PercentUtil.myPercent(toWeekPayCount-yesWeekPayCount, yesWeekPayCount);
            list.add(week);
        }else{
            list.add("0%");
        }
        return list;
    }

    @Override
    public List getDayForecast() {

        List<String> list = new ArrayList<>();


        int dayPassenger = orderMapper.getDayPassenger();//当日客流量
        List<String> monthDayAvg = orderMapper.get2020MonthDayAvg();//2020年日平均值
        Integer month = DateUtils.getMonth(new Date());//获取当前月份
        String avg = monthDayAvg.get(month - 1);//当月平均值

        list.add(Integer.parseInt(avg)-dayPassenger+"");//日客流量预测
        list.add(Integer.parseInt(avg)*30+"");//月客流量预测

        return list;
    }

    @Override
    public int getDayPassenger() {
        return orderMapper.getDayPassenger();
    }

    @Override
    public List get2020MonthDayAvg() {
        return orderMapper.get2020MonthDayAvg();
    }


}
